\documentclass{article}

\begin{document}
\SweaveOpts{concordance=TRUE}

<<>>=
load("/Users/hannahridge/Dropbox/CSES Satsifaction/CSES Satsifaction/cses2.rdata")
#load cses2.rdata from CSES
@

<<>>=
# Removing Hong Kong, Taiwan, Russia, Kyrgyzstan
cses2d <- subset(cses2, cses2$B1006_NAM != 'Hong Kong' & cses2$B1006_NAM != "Taiwan" & cses2$B1006_NAM != "Russian Federation")
@

<<>>=
### Getting rid of the Refused, Don't Know, and Missing responses in the age variable
cses2d <- subset(cses2d, cses2$B2001  < 997)

cses2d$aged <- NA
cses2d$aged[cses2d$B2001>="16" & cses2d$B2001 <="29"] <- "1"
cses2d$aged[cses2d$B2001>="30" & cses2d$B2001 <="39"] <- "2"
cses2d$aged[cses2d$B2001>="40" & cses2d$B2001 <="49"] <- "3"
cses2d$aged[cses2d$B2001>="50" & cses2d$B2001] <- "4"
cses2d$aged[cses2d$B2001=="1"] <- "1"
cses2d$aged[cses2d$B2001=="2"] <- "2"
cses2d$aged[cses2d$B2001=="3"] <- "3"
cses2d$aged[cses2d$B2001=="4"] <- "4"

### Getting rid of the Refused and Missing responses in the Gender variable
cses2d <- subset(cses2d, cses2d$B2002 <= 2)

### Getting rid of [see election study notes] and Refused, Don't Know, and Missing responses to the Education variable
cses2d <- subset(cses2d, cses2d$B2003 < 9)

### Removing Refused, Don't Know, Missing, and [See election study notes] responses from the SWD variable
cses2d <- subset(cses2d, cses2d$B3012 < 6)

### Turning outcome variable (SWD) into a factor
cses2d$B3012 <- as.factor(cses2d$B3012)

### Turning Gender variable into a factor
cses2d$B2002 <- as.factor(cses2d$B2002)

### Creating a binary employed/unemployed variable
cses2d$unemployed <- NA
cses2d$unemployed <- ifelse(cses2d$B2010 == 5, 1, 0)
cses2d$unemployed <- as.factor(cses2d$unemployed)

### Getting rid of [see election study notes] and Refused, Don't Know, and Missing responses to the ideology variable
cses2d <- subset(cses2d, cses2d$B3045 < 95)

###Getting rid of [see election study notes] and Refused, Don't Know, and Missing responses to the income quintile variable
cses2d <- subset(cses2d, cses2d$B2020 < 6)
@

<<>>=
### Recoding two German samples into one
cses2d$B1004[cses2d$B1004=="DEU12002"] <- "DEU_2002"
cses2d$B1004[cses2d$B1004=="DEU22002"] <- "DEU_2002"
@

<<>>=
### add GDP per capita, PPP (constant 2011 international $) from World Bank
cses2d$gdp <- NA
cses2d$gdp <- ifelse(cses2d$B1004 == "ALB_2005", 7000.503271, cses2d$gdp)
cses2d$gdp <- ifelse(cses2d$B1004 == "AUS_2004", 37183.68457, cses2d$gdp)
cses2d$gdp <- ifelse(cses2d$B1004 == "BEL_2003", 37856.86471, cses2d$gdp)
cses2d$gdp <- ifelse(cses2d$B1004 == "BGR_2001", 8833.405484, cses2d$gdp)
cses2d$gdp <- ifelse(cses2d$B1004 == "BRA_2002", 11368.46645, cses2d$gdp)
cses2d$gdp <- ifelse(cses2d$B1004 == "CAN_2004", 38805.67111, cses2d$gdp)
cses2d$gdp <- ifelse(cses2d$B1004 == "CHE_2003", 50815.15316, cses2d$gdp)
cses2d$gdp <- ifelse(cses2d$B1004 == "CHL_2005", 16258.38895, cses2d$gdp)
cses2d$gdp <- ifelse(cses2d$B1004 == "CZE_2002", 21892.48253, cses2d$gdp)
cses2d$gdp <- ifelse(cses2d$B1004 == "DEU_2002", 37325.05255, cses2d$gdp)
cses2d$gdp <- ifelse(cses2d$B1004 == "DNK_2001", 42337.71437, cses2d$gdp)
cses2d$gdp <- ifelse(cses2d$B1004 == "ESP_2004", 31815.42134, cses2d$gdp)
cses2d$gdp <- ifelse(cses2d$B1004 == "FIN_2003", 36218.24016, cses2d$gdp)
cses2d$gdp <- ifelse(cses2d$B1004 == "FRA_2002", 35304.73072, cses2d$gdp)
cses2d$gdp <- ifelse(cses2d$B1004 == "GBR_2005", 36292.18203, cses2d$gdp)
cses2d$gdp <- ifelse(cses2d$B1004 == 'HUN_2002', 18683.98185, cses2d$gdp)
cses2d$gdp <- ifelse(cses2d$B1004 == 'IRL_2002', 40455.30120, cses2d$gdp)
cses2d$gdp <- ifelse(cses2d$B1004 == 'ISL_2003', 34794.78788, cses2d$gdp)
cses2d$gdp <- ifelse(cses2d$B1004 == 'ISR_2003', 25725.46735, cses2d$gdp)
cses2d$gdp <- ifelse(cses2d$B1004 == 'ITA_2006', 37604.36062, cses2d$gdp)
cses2d$gdp <- ifelse(cses2d$B1004 == 'JPN_2004', 34333.13367, cses2d$gdp)
cses2d$gdp <- ifelse(cses2d$B1004 == 'KGZ_2005', 2401.265004, cses2d$gdp)
cses2d$gdp <- ifelse(cses2d$B1004 == 'KOR_2004', 23549.36645, cses2d$gdp)
cses2d$gdp <- ifelse(cses2d$B1004 == 'MEX_2003', 15218.79392, cses2d$gdp)
cses2d$gdp <- ifelse(cses2d$B1004 == 'NLD_2002', 42287.56189, cses2d$gdp)
cses2d$gdp <- ifelse(cses2d$B1004 == 'NOR_2001', 58045.06727, cses2d$gdp)
cses2d$gdp <- ifelse(cses2d$B1004 == 'NZL_2002', 28500.95264, cses2d$gdp)
cses2d$gdp <- ifelse(cses2d$B1004 == 'PER_2006', 6978.570061, cses2d$gdp)  
cses2d$gdp <- ifelse(cses2d$B1004 == 'PHL_2004', 4441.675324, cses2d$gdp)
cses2d$gdp <- ifelse(cses2d$B1004 == 'POL_2001', 14732.48200, cses2d$gdp)
cses2d$gdp <- ifelse(cses2d$B1004 == 'PRT_2002', 26318.47816, cses2d$gdp)
cses2d$gdp <- ifelse(cses2d$B1004 == 'PRT_2005', 26439.93318, cses2d$gdp)
cses2d$gdp <- ifelse(cses2d$B1004 == 'ROU_2004', 107615.8955, cses2d$gdp)  
cses2d$gdp <- ifelse(cses2d$B1004 == 'RUS_2004', 16818.74263, cses2d$gdp)
cses2d$gdp <- ifelse(cses2d$B1004 == 'SVN_2004', 24896.11909, cses2d$gdp)
cses2d$gdp <- ifelse(cses2d$B1004 == 'SWE_2002', 37330.70497, cses2d$gdp)
cses2d$gdp <- ifelse(cses2d$B1004 == 'USA_2004', 47260.04194, cses2d$gdp)
@

<<Dv>>=
### Recoding dependent variable
### Dependent variable is now: 1) Not At All Satisfied; 2) Not Very Satisfied; 3) Fairly Satisfied; 4) Very Satisfied

cses2d$B3012 <- recode(cses2d$B3012,"4=1;3=2;2=3;1=4")
@

<<>>=
cses2a <- subset(cses2d, as.numeric(cses2d$B3015) < 5)

### Recoding dependent variable
cses2a$B3015 <- recode(cses2a$B3015,"4=1;3=2;2=3;1=4")
### Dependent variable is now: 1) disagree strongly; 2) disagree 3) agree; 4) agree strongly
cses2a$B3015 <- as.factor(cses2a$B3015)

db1 <- clmm(B3015 ~ as.numeric(B3012) + scale(B2001) + B2002 + scale(gdp) + scale(B2003) + B2020 + B3045 + (1 | B1004), data = cses2a, link="logit")
summary(db1) 

db2 <- clmm(B3015 ~ B3012 + scale(B2001) + B2002 + scale(gdp) + scale(B2003) + B2020 + B3045 + (1 | B1004), data = cses2a, link="logit")
summary(db2) 

htmlreg(list(db1, db2))
@

\end{document}